package com.jd.charactercalculator.controller;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.jd.charactercalculator.controller.common.BaseAjaxController;
import com.jd.charactercalculator.controller.common.JsonResult;
import com.jd.charactercalculator.dto.param.CharacterParamDto;
import com.jd.charactercalculator.dto.result.CharacterResultDto;
import com.jd.charactercalculator.dto.result.PagingResultDto;
import com.jd.charactercalculator.facade.CharacterFacade;
import com.jd.charactercalculator.service.CharacterService;
import com.jd.charactercalculator.util.BeanUtils;


/**
 * @ClassName:CharactorTestContoller
 * @Description: 
 * @author :孙林
 * @date :2012-9-1上午10:13:10
 */
public class CharacterContoller extends BaseAjaxController{

	private CharacterFacade characterFacade;
	
	private String viewName;
	private CharacterService characterService;
	
	public ModelAndView characterTest(HttpServletRequest request, HttpServletResponse respnose) throws UnsupportedEncodingException{
//		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
//		int score = 4;
//		String evaluation = "算了，跟你没什么人品好谈的...";
//		String message = "\n姓名：" + username + 
//						 "\n人品：" + score  + 
//						 "\n评价：" + evaluation;
//		logger.info(message);	
//		Map<String, String> model = new HashMap<String,String>();
//		model.put("message", message);
//		return new ModelAndView(getViewName(),model);
		CharacterParamDto characterParamDto = new CharacterParamDto();
		//获取人品
		PagingResultDto<CharacterResultDto> result =  characterFacade.getCharacters(characterParamDto);
		int hashCode = username.hashCode();
		
		int score = hashCode%100;
		//根据得分，获取人品评价
		String evaluation ="";
		int type = (int) (score/3.85);
		List<CharacterResultDto> characterResultDtoList = result.getList();
		Iterator<CharacterResultDto> iterator =  characterResultDtoList.iterator();
		while (iterator.hasNext()) {
			CharacterResultDto characterResultDto = iterator.next();
			if(String.valueOf(type).equalsIgnoreCase(characterResultDto.getType())){
				evaluation = characterResultDto.getEvaluation();
				break;
			}
			
		}
//		String message = "姓名：" + username + "人品：" + score  + "评价：" + evaluation;
//		System.out.println(message);
		return new ModelAndView(getViewName());
	}
	
	//显示所有人品
	public void showCharacter(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{

		CharacterParamDto characterParamDto = new CharacterParamDto();
		bindObject(request, characterParamDto);
		//获取人品
		PagingResultDto<CharacterResultDto> result =  characterFacade.getCharacters(characterParamDto);
		
		JsonResult jr = new JsonResult();
		jr.setList(result.getList());
		jr.setTotalSize(result.getTotalSize());
		jr.setSuccess(true);
		System.out.println("JsonResult : " + BeanUtils.bean2Json(jr));
		this.outJsonString(response, jr);
	}
	
	public String getViewName() {
		return viewName;
	}

	public void setViewName(String viewName) {
		this.viewName = viewName;
	}

	public CharacterFacade getCharacterFacade() {
		return characterFacade;
	}

	public void setCharacterFacade(CharacterFacade characterFacade) {
		this.characterFacade = characterFacade;
	}

	public CharacterService getCharacterService() {
		return characterService;
	}

	public void setCharacterService(CharacterService characterService) {
		this.characterService = characterService;
	}
	
	
	
}
